package com.bw.server.device

import android.view.View
import androidx.activity.viewModels
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.path.RouterPath
import com.bw.common.view.BaseMvvmActivity
import com.bw.server.R
import com.bw.server.adapter.DwAdapter
import com.bw.server.adapter.GdAdapter
import com.bw.server.adapter.ItemEntity
import com.bw.server.databinding.ActivityDetailNormalDeviceBinding
import com.bw.server.vm.ServerViewModel
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import dagger.hilt.android.AndroidEntryPoint

/**
 * 正常状态的设备详情
 */
@Route(path = RouterPath.PATH_DEVICE_DETAIL_NORMAL)
@AndroidEntryPoint
class DetailNormalDeviceActivity : BaseMvvmActivity<ActivityDetailNormalDeviceBinding>() {

    @Autowired
    @JvmField
    var did:Int = 0
    var dInspector:String = ""
    var dwAdapter = DwAdapter()
    var gdAdapter = GdAdapter()
    private val mViewModel:ServerViewModel by viewModels()
    override fun getLayoutId(): Int = R.layout.activity_detail_normal_device

    override fun initViews() {
        super.initViews()
        ARouter.getInstance().inject(this)
        ToastUtils.showLong("id${did}")
        mDataBinding.rv.adapter = dwAdapter
        mDataBinding.tab.addTab(mDataBinding.tab.newTab().setText("点位信息"))
        mDataBinding.tab.addTab(mDataBinding.tab.newTab().setText("跟踪信息"))
        val list = listOf(
            ItemEntity("西区游乐场", "游乐场", "未巡检》"),
            ItemEntity("东区健身区", "健身区", "未巡检》"),
            ItemEntity("南区游泳池", "游泳池", "未巡检》"),
        )

        dwAdapter.setList(list)

        mDataBinding.tab.addOnTabSelectedListener(object: OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                when(tab?.position){
                    0->{
                        mDataBinding.rv.adapter = dwAdapter
                        mDataBinding.assignTasks.visibility= View.VISIBLE
                        mDataBinding.assignTasks.setOnClickListener {
                            mDataBinding.startInspection.visibility=View.VISIBLE
                            mDataBinding.transfer.visibility=View.VISIBLE
                            mDataBinding.assignTasks.visibility=View.GONE
                        }
                    }

                    1->{
                        mDataBinding.ll.visibility=View.GONE
                        val list1 = listOf<String>(
                            "暂无数据",
                        )
//                        mDataBinding.rv.adapter = gdAdapter
//                        gdAdapter.data.clear()
//                        gdAdapter.addData(list1)
//                        gdAdapter.notifyDataSetChanged()

                        mDataBinding.assignTasks.visibility= View.GONE
                    }
                }
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
            }

        })

        mViewModel.mDetailSuccess.observe(this){
            mDataBinding.detailName.text = it.orderType
            mDataBinding.detailTime.text = it.appointmentTime

            if (it.status == "进行中"){
                mDataBinding.detailZt.text = "已分配"
                mDataBinding.detailZt.background = getDrawable(R.drawable.device_zt1)
            }else if (it.status == "已完成"){
                mDataBinding.detailZt.text = "已过期"
                mDataBinding.detailZt.background = getDrawable(R.drawable.device_zt5)
            }else if (it.status == "已转派"){
                mDataBinding.detailZt.text = "已转派"
                mDataBinding.detailZt.background = getDrawable(R.drawable.device_zt3)
            }else{
                mDataBinding.detailZt.text = "未分配"
                mDataBinding.detailZt.background = getDrawable(R.drawable.device_zt4)
                mDataBinding.assignTasks.visibility = View.VISIBLE
            }
        }

        mDataBinding.assignTasks.setOnClickListener {
            mDataBinding.startInspection.visibility=View.VISIBLE
            mDataBinding.transfer.visibility=View.VISIBLE
            mDataBinding.assignTasks.visibility=View.GONE
        }

        //开始巡检
        mDataBinding.startInspection.setOnClickListener {
            mViewModel.startInspection(did)
            mViewModel.mStartSuccess.observe(this){
                ToastUtils.showLong("开始巡检成功")
                mDataBinding.detailZt.text = "进行中"
                mDataBinding.detailZt.setBackgroundResource(R.drawable.device_zt1)
            }
        }
        //转派
        mDataBinding.transfer.setOnClickListener {
            mViewModel.transfer(did,dInspector)
            mViewModel.mTransferSuccess.observe(this){
                ToastUtils.showLong("转派成功")
            }
        }

    }

    override fun loadData() {
        super.loadData()
        mViewModel.getDetailData(did)
    }




}